package com.ruoyi.wechat.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.service.IFileService;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.domain.WechatUserPointsRecord;
import com.ruoyi.wechat.mapper.WechatUserMapper;
import com.ruoyi.wechat.mapper.WechatUserPointsRecordMapper;
import com.ruoyi.wechat.service.IWechatUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.ruoyi.common.constant.Constants.DEFAULT_AVATAR;

/**
 * 微信用户Service业务层处理
 *
 * @author KevenPotter
 * @date 2024-04-26 23:59:38
 */
@Service
@AllArgsConstructor
public class WechatUserServiceImpl implements IWechatUserService {

    private final IFileService fileService;
    private final WechatUserMapper wechatUserMapper;
    private final WechatUserPointsRecordMapper wechatUserPointsRecordMapper;

    /**
     * 查询微信用户
     *
     * @param id 微信用户主键
     * @return 微信用户
     */
    @Override
    public WechatUser getById(Long id) {
        return wechatUserMapper.getById(id);
    }

    /**
     * 根据[手机号码]获取微信用户
     *
     * @param mobile 手机号码
     * @return 返回微信用户
     * @author KevenPotter
     * @date 2024-04-29 23:14:27
     */
    @Override
    public WechatUser getByMobile(String mobile) {
        return wechatUserMapper.getByMobile(mobile);
    }

    /**
     * 查询微信用户列表
     *
     * @param wechatUser 微信用户
     * @return 微信用户
     */
    @Override
    public List<WechatUser> getList(WechatUser wechatUser) {
        return wechatUserMapper.getList(wechatUser);
    }

    /**
     * 查询所有微信用户
     *
     * @return 返回所有微信用户
     * @author KevenPotter
     * @date 2024-04-29 15:55:11
     */
    @Override
    public List<WechatUser> getAllList() {
        return wechatUserMapper.getAllList();
    }

    /**
     * 新增微信用户
     *
     * @param wechatUser 微信用户
     * @return 结果
     */
    @Override
    public int insertWechatUser(WechatUser wechatUser) {
        wechatUser.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(wechatUser.getAvatar())) wechatUser.setAvatar(DEFAULT_AVATAR);
        return wechatUserMapper.insertWechatUser(wechatUser);
    }

    /**
     * 修改微信用户
     *
     * @param wechatUser 微信用户
     * @return 结果
     */
    @Override
    public int updateWechatUser(WechatUser wechatUser) {
        return wechatUserMapper.updateWechatUser(wechatUser);
    }

    /**
     * 批量删除微信用户
     *
     * @param ids 需要删除的微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWechatUserByIds(Long[] ids) {
        return wechatUserMapper.deleteWechatUserByIds(ids);
    }

    /**
     * 删除微信用户信息
     *
     * @param id 微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWechatUserById(Long id) {
        return wechatUserMapper.deleteWechatUserById(id);
    }

    /**
     * 查询微信用户总数
     *
     * @return 返回微信用户总数
     * @author KevenPotter
     * @date 2024-05-01 20:08:08
     */
    @Override
    public int getWechatUserCounts() {
        return wechatUserMapper.getWechatUserCounts();
    }

    /**
     * 修改微信用户积分
     *
     * @param userId 用户编号
     * @param points 积分
     * @return 返回是否修改成功
     * @author KevenPotter
     * @date 2024-05-02 23:41:35
     */
    @Override
    public int setPoints(Long userId, Integer points) {
        return wechatUserMapper.setPoints(userId, points);
    }

    /**
     * 派送微信用户积分
     *
     * @param wechatUserPointsRecord 微信用户积分记录对象
     * @return 返回是否修改成功
     * @author KevenPotter
     * @date 2024-07-17 10:22:13
     */
    @Override
    public int dispatchPoints(WechatUserPointsRecord wechatUserPointsRecord, LoginUser loginUser) {
        Long userId = wechatUserPointsRecord.getUserId();
        Integer points = wechatUserPointsRecord.getPoints();
        WechatUser wechatUser = wechatUserMapper.getById(userId);
        Integer beforePoints = wechatUser.getPoints();
        int afterPoints = 0;
        WechatUserPointsRecord record = new WechatUserPointsRecord()
                .setUserId(userId).setBeforePoints(beforePoints).setPoints(points);
        String type = wechatUserPointsRecord.getType();
        if ("1".equals(type)) {
            afterPoints = wechatUser.getPoints() + wechatUserPointsRecord.getPoints();
            record.setAfterPoints(afterPoints).setDesc("派送" + points + "积分");
        } else if ("2".equals(type)) {
            afterPoints = wechatUser.getPoints() - wechatUserPointsRecord.getPoints();
            record.setAfterPoints(afterPoints).setDesc("减少" + points + "积分");
        }
        record.setCreateBy(loginUser.getUserId()).setCreateTime(new Date());
        int i = wechatUserPointsRecordMapper.insertWechatUserPointsRecord(record);
        wechatUserMapper.setPoints(userId, afterPoints);
        return i;
    }

    /**
     * 过滤出埠参数参数
     *
     * @param wechatUser 微信用户对象
     * @param loginUser  当前登录对象
     * @return 返回过滤后的[微信用户对象]
     * @author KevenPotter
     * @date 2024-04-28 13:35:23
     */
    @Override
    public WechatUser filterInbound(WechatUser wechatUser, LoginUser loginUser) {
        if (StringUtils.isNotEmpty(wechatUser.getAvatar())) wechatUser.setAvatar(FileUtils.buildFileUrl(wechatUser.getAvatar()));
        return wechatUser;
    }

    /**
     * 过滤出埠参数参数
     *
     * @param wechatUser 微信用户对象
     * @param loginUser  当前登录对象
     * @return 返回过滤后的[微信用户对象]
     * @author KevenPotter
     * @date 2024-04-28 13:35:29
     */
    @Override
    public WechatUser filterOutbound(WechatUser wechatUser, LoginUser loginUser) {
        if (StringUtils.isNotEmpty(wechatUser.getAvatar()) && !wechatUser.getAvatar().contains("https")) wechatUser.setAvatar(fileService.buildSingleFileUrlFullPath(wechatUser.getAvatar()));
        return wechatUser;
    }
}
